/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 14-abr-2009, 19:14:51
 */

package brutot;

import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Scar
 */
public class Principal extends javax.swing.JFrame {
public String path;
public int ampladaImatge;
public int alturaImatge;
    /** Creates new form Principal */
    public Principal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dlgObrir = new javax.swing.JFileChooser();
        btnObrir = new javax.swing.JButton();
        txtAmplada = new javax.swing.JTextField();
        lblx = new javax.swing.JLabel();
        txtAltura = new javax.swing.JTextField();
        lblPixels = new javax.swing.JLabel();
        btnComencar = new javax.swing.JButton();

        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "JPG & GIF & PNG Images", "jpg", "gif","png");
        File dir = new File("C:/Users/Scar/Desktop");
        dlgObrir.setFileFilter(filter);
        dlgObrir.setCurrentDirectory(dir);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        btnObrir.setText("Obrir");
        btnObrir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnObrirMouseClicked(evt);
            }
        });

        txtAmplada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAmpladaActionPerformed(evt);
            }
        });

        lblx.setText("x");

        lblPixels.setText("Pixels");

        btnComencar.setText("Començar");
        btnComencar.setEnabled(false);
        btnComencar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnComencarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtAmplada)
                            .addComponent(btnObrir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblx)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAltura, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblPixels))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(72, 72, 72)
                        .addComponent(btnComencar)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(btnObrir)
                .addGap(93, 93, 93)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAmplada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblx, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAltura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPixels))
                .addGap(18, 18, 18)
                .addComponent(btnComencar)
                .addContainerGap(44, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtAmpladaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAmpladaActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtAmpladaActionPerformed

    private void btnObrirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnObrirMouseClicked
        // TODO add your handling code here:
        dlgObrir.setVisible(true);
        int returnVal = dlgObrir.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            File file = dlgObrir.getSelectedFile();
            path=file.getAbsolutePath();
           //System.out.println("Absolute"+ file.getAbsolutePath());


            //This is where a real application would open the file.
            btnComencar.setEnabled(true);

        }

}//GEN-LAST:event_btnObrirMouseClicked

    @SuppressWarnings("static-access")
    private void btnComencarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnComencarMouseClicked
        // TODO add your handling code here:


        frameImatge f= new frameImatge();
        f.agafa_path(path);
        ImageIcon imatgeOriginal = new ImageIcon(path);
        txtAmplada.setText(String.valueOf(imatgeOriginal.getIconWidth()));
        txtAltura.setText(String.valueOf(imatgeOriginal.getIconHeight()));
        f.setSize(imatgeOriginal.getIconWidth()+16,imatgeOriginal.getIconHeight()+36);
        f.setVisible(true);
        

}//GEN-LAST:event_btnComencarMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnComencar;
    private javax.swing.JButton btnObrir;
    private javax.swing.JFileChooser dlgObrir;
    private javax.swing.JLabel lblPixels;
    private javax.swing.JLabel lblx;
    private javax.swing.JTextField txtAltura;
    private javax.swing.JTextField txtAmplada;
    // End of variables declaration//GEN-END:variables

}
